package cn.iocoder.yudao.module.ao.service.curriculum;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.curriculum.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.curriculum.CurriculumDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.curriculum.CurriculumMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 课程体系 Service 实现类
 *
 * @author 智教云枢
 */
@Service
@Validated
public class CurriculumServiceImpl implements CurriculumService {

    @Resource
    private CurriculumMapper curriculumMapper;

    @Override
    public Long createCurriculum(CurriculumSaveReqVO createReqVO) {
        // 插入
        CurriculumDO curriculum = BeanUtils.toBean(createReqVO, CurriculumDO.class);
        curriculumMapper.insert(curriculum);

        // 返回
        return curriculum.getCurriculumId();
    }

    @Override
    public void updateCurriculum(CurriculumSaveReqVO updateReqVO) {
        // 校验存在
        validateCurriculumExists(updateReqVO.getCurriculumId());
        // 更新
        CurriculumDO updateObj = BeanUtils.toBean(updateReqVO, CurriculumDO.class);
        curriculumMapper.updateById(updateObj);
    }

    @Override
    public void deleteCurriculum(Long id) {
        // 校验存在
        validateCurriculumExists(id);
        // 删除
        curriculumMapper.deleteById(id);
    }

    @Override
        public void deleteCurriculumListByIds(List<Long> ids) {
        // 删除
        curriculumMapper.deleteByIds(ids);
        }


    private void validateCurriculumExists(Long id) {
        if (curriculumMapper.selectById(id) == null) {
            throw exception(CURRICULUM_NOT_EXISTS);
        }
    }

    @Override
    public CurriculumDO getCurriculum(Long id) {
        return curriculumMapper.selectById(id);
    }

    @Override
    public PageResult<CurriculumDO> getCurriculumPage(CurriculumPageReqVO pageReqVO) {
        return curriculumMapper.selectPage(pageReqVO);
    }

}